<?php

include_once('countries.php');
include_once('langCodeISO639-2.php');
require_once('gbif.eml.php');

class CustomEml extends Eml {
	public function createEmlFromWorksheet( $wk, $extra = false ) {

		build3DigitLanguageCode();
		build2DigitCountryCode();
		
		#Basic Metadata	
// 		$this->set('ResourceType', $wk->getCell('B2')->getValue() );
		$this->set('ResourceUUID', $wk->getCell('B3')->getValue() );
		$this->set('ResourceTitle', $wk->getCell('B4')->getValue() );
		$this->set('ResourcePublicationDate', date("Y-m-d", PHPExcel_Shared_Date::ExcelToPHP( $wk->getCell('B5')->getValue())) );
		$this->set('ResourceExpectedCitation', $wk->getCell('B6')->getValue() );
		$this->set('ResourceAbstract', $wk->getCell('B7')->getValue() );
		$this->set('ResourceAdditionalInformation', $wk->getCell('B8')->getValue() );
		$this->set('ResourceLanguage', get3DigitLanguageCode( $wk->getCell('B9')->getValue() ) );
		$this->set('ResourceURL', $wk->getCell('B10')->getValue() );
		$this->set('ResourceCreativeCommonsLicensing', $wk->getCell('B11')->getValue() );
		$this->set('MetadataLanguage', get3DigitLanguageCode( $wk->getCell('F9')->getValue() ) );
		$this->set('ResourceLogoURL', $wk->getCell('F10')->getValue() );

		#Research Project
		$this->set('ProjectTitle', $wk->getCell('J2')->getValue() );
		$this->set('ProjectLeadOrganisation', $wk->getCell('J3')->getValue() );
		$this->set('ProjectLeadOrganisationCountry', get2DigitCountryCode( $wk->getCell('M3')->getValue() ) );
		$this->set('ProjectDescription', $wk->getCell('J4')->getValue() );

		#People and Organisations
		$this->set('MetadataAuthor.FirstName', $wk->getCell('B18')->getValue() );
		$this->set('MetadataAuthor.LastName', $wk->getCell('C18')->getValue() );
		$this->set('MetadataAuthor.Organisation', $wk->getCell('D18')->getValue() );
		$this->set('MetadataAuthor.Phone', $wk->getCell('E18')->getValue() );
		$this->set('MetadataAuthor.Email', $wk->getCell('F18')->getValue() );
		$this->set('MetadataAuthor.Homepage', $wk->getCell('G18')->getValue() );
		$this->set('MetadataAuthor.Address', $wk->getCell('H18')->getValue() );
		$this->set('MetadataAuthor.City', $wk->getCell('I18')->getValue() );
		$this->set('MetadataAuthor.Province', $wk->getCell('J18')->getValue() );
		$this->set('MetadataAuthor.Country', get2DigitCountryCode( $wk->getCell('K18')->getValue() ) );
		$this->set('MetadataAuthor.PostalCode', $wk->getCell('L18')->getValue() );
		$this->set('MetadataAuthor.PrimaryContact', $wk->getCell('M18')->getValue() );

		#People and Organisations
		$this->set('ResourceCreator.FirstName', $wk->getCell('B17')->getValue() );
		$this->set('ResourceCreator.LastName', $wk->getCell('C17')->getValue() );
		$this->set('ResourceCreator.Organisation', $wk->getCell('D17')->getValue() );
		$this->set('ResourceCreator.Phone', $wk->getCell('E17')->getValue() );
		$this->set('ResourceCreator.Email', $wk->getCell('F17')->getValue() );
		$this->set('ResourceCreator.Homepage', $wk->getCell('G17')->getValue() );
		$this->set('ResourceCreator.Address', $wk->getCell('H17')->getValue() );
		$this->set('ResourceCreator.City', $wk->getCell('I17')->getValue() );
		$this->set('ResourceCreator.Province', $wk->getCell('J17')->getValue() );
		$this->set('ResourceCreator.Country', get2DigitCountryCode( $wk->getCell('K17')->getValue() ) );
		$this->set('ResourceCreator.PostalCode', $wk->getCell('L17')->getValue() );
		$this->set('ResourceCreator.PrimaryContact', $wk->getCell('M17')->getValue() );

		#People and Organisations
// 		$this->set('Author.FirstName', $wk->getCell('B19')->getValue() );
// 		$this->set('Author.LastName', $wk->getCell('C19')->getValue() );
// 		$this->set('Author.Organisation', $wk->getCell('D19')->getValue() );
// 		$this->set('Author.Phone', $wk->getCell('E19')->getValue() );
// 		$this->set('Author.Email', $wk->getCell('F19')->getValue() );
// 		$this->set('Author.Homepage', $wk->getCell('G19')->getValue() );
// 		$this->set('Author.Address', $wk->getCell('H19')->getValue() );
// 		$this->set('Author.City', $wk->getCell('I19')->getValue() );
// 		$this->set('Author.Province', $wk->getCell('J19')->getValue() );
// 		$this->set('Author.Country', $wk->getCell('K19')->getValue() );
// 		$this->set('Author.PostalCode', $wk->getCell('L19')->getValue() );

		$authors = array();
		for($i = 19; $i <= 30; $i++) {
			$authFlag = false;
			for($j=66;$j<=76;$j++) {
				if(trim($wk->getCell(chr($j).$i)->getValue()) != '') {
					$authFlag = true;break;
				}
			}
			if($authFlag) {
				$authors[] = array(
						'Role' =>  $wk->getCell('A'.$i)->getValue()
					,	'FirstName' =>  $wk->getCell('B'.$i)->getValue()
					, 'LastName' => $wk->getCell('C'.$i)->getValue()
					, 'Organisation' => $wk->getCell('D'.$i)->getValue()
					, 'Phone' => $wk->getCell('E'.$i)->getValue()
					, 'Email' => $wk->getCell('F'.$i)->getValue()
					, 'Homepage' => $wk->getCell('G'.$i)->getValue()
					, 'Address' => $wk->getCell('H'.$i)->getValue()
					, 'City' => $wk->getCell('I'.$i)->getValue()
					, 'Province' => $wk->getCell('J'.$i)->getValue()
					, 'Country' => get2DigitCountryCode( $wk->getCell('K'.$i)->getValue() )
					, 'PostalCode' => $wk->getCell('L'.$i)->getValue()
				);
			} # authflag
		}
		$this->set('authors', $authors);

		#Geographical,Taxonomic and General Keywords	
		$this->set('RegionalScope', $wk->getCell('B36')->getValue() );
		$this->set('GeneralKeywords', $wk->getCell('B37')->getValue() );
		$tmp = $wk->getCell('B37')->getValue();
		if  ($tmp != "") {
			$this->set('KeyWords',@explode(',',$tmp));
		} else {
			$this->set('KeyWords', "");
		}
		$this->set('KeywordThesaurus', $wk->getCell('B38')->getValue() );
		$this->set('BeginDateorSingleDate', date("Y-m-d", PHPExcel_Shared_Date::ExcelToPHP($wk->getCell('B39')->getValue()) ) );
		$this->set('EndDate', date("Y-m-d", PHPExcel_Shared_Date::ExcelToPHP($wk->getCell('B40')->getValue()) ) );

		$this->set('BoundingBoxLeftLat', $wk->getCell('G37')->getValue() );
		$this->set('BoundingBoxLeftLong', $wk->getCell('H37')->getValue() );
		$this->set('BoundingBoxRightLat', $wk->getCell('G38')->getValue() );
		$this->set('BoundingBoxRightLong', $wk->getCell('H38')->getValue() );

		# References

		$references = array();
		$refFlag = true;
		$i = 46;
		while($refFlag) {
			if(@strtolower(trim($wk->getCell('A'.$i)->getValue())) == 'reference') {
				if(trim($wk->getCell('B'.$i)->getValue()) != '') {
// 					$references[] = array('reference' => $wk->getCell('B'.$i)->getValue());
					$references[] = $wk->getCell('B'.$i)->getValue();
				}
			} else {
				$refFlag = false;
			}
			$i++;
		}
		$this->set('references', $references);

		if ($extra) {
			$this->set('CollectionName', $wk->getCell('B60')->getValue() );		
			$this->set('CollectionIdentifier', $wk->getCell('B61')->getValue() );		
			$this->set('ParentCollectionIdentifier', $wk->getCell('B62')->getValue() );		
			$this->set('FormationPeriod', $wk->getCell('B63')->getValue() );		
			$this->set('LivingTimePeriod', $wk->getCell('B64')->getValue() );		
			$this->set('SpecimenPreservationMethod', $wk->getCell('B65')->getValue() );		
		}
/*
		#Methods	
		$this->set('StudyExtent', $wk->getCell('D38')->getValue() );
		$this->set('Sampling', $wk->getCell('D39')->getValue() );

		#Method Steps
//

		#IPR
		$this->set('Purpose', $wk->getCell('B50')->getValue() );
		$this->set('Maintenance', $wk->getCell('B51')->getValue() );
		$this->set('IPRRights', $wk->getCell('B52')->getValue() );
		$this->set('BibliographicCitation', $wk->getCell('B53')->getValue() );
		$this->set('References(1)', $wk->getCell('B54')->getValue() );
		$this->set('References(2)', $wk->getCell('B55')->getValue() );
		$this->set('References(3)', $wk->getCell('B56')->getValue() );

		#Collections-related Metadata
		$this->set('CollectionName', $wk->getCell('B61')->getValue() );
		$this->set('CollectionIdentifier', $wk->getCell('B62')->getValue() );
		$this->set('ParentCollectionIdentifier', $wk->getCell('B63')->getValue() );
		$this->set('FormationPeriod', $wk->getCell('B64')->getValue() );
		$this->set('LivingTimePeriod', $wk->getCell('B65')->getValue() );
		$this->set('SpecimenPreservationMethod', $wk->getCell('B66')->getValue() );
*/
	}
}
?>